package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.Rule;
import com.rykj.qxj.server.services.IRuleService;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Log4j
@RestController
@RequestMapping("/v1/rule")
@Api(tags="字段校验规则")
public class RuleController {
    @Autowired
    private IRuleService ruleService;

    @PostMapping("/saveRule")
    @ApiOperation(value="添加规则",notes ="添加规则")
    @EnableSystemLog(value="修改阅读数量",fullPath ="/v1/rule/saveRule")
    public Result saveRule(@RequestBody Rule rule){
        try{
            ruleService.saveRule(rule);
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "添加失败");
        }
    }


    @PostMapping("/updateRule")
    @ApiOperation(value="修改规则",notes ="修改规则")
    @EnableSystemLog(value="修改规则",fullPath ="/v1/rule/updateRule")
    public Result updateRule(@RequestBody Rule rule){
        try{
            ruleService.updateRule(rule);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }

    @GetMapping("/deleteRule")
    @ApiOperation(value="删除规则",notes ="删除规则")
    @EnableSystemLog(value="删除规则",fullPath ="/v1/rule/deleteRule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id主键",required = true,paramType = "query")
    })
    public Result deleteRule(@RequestParam(value = "id") Integer id){
        try{
            ruleService.deleteRule(id);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }

    @GetMapping("/getRuleDetails")
    @ApiOperation(value="获取规则详情",notes ="获取规则详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = " id主键",required = true,paramType = "query")
    })
    public Result getRuleDetails(@RequestParam(value = "id") Integer id){
        try{
           Rule rule = ruleService.getRuleDetails(id);
            return new Result(rule);
        }catch (Exception e){
            log.error("获取集市目录失败", e);
            return new Result(false, "获取集市目录失败");
        }
    }

    @GetMapping("getRuleList")
    @ApiOperation(value="分页获取规则列表",notes ="分页获取规则列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "ruleID", value = "规则ID",paramType = "query"),
            @ApiImplicitParam(name = "ruleName", value = "规则名称",paramType = "query")
    })
    public Result getRuleList(@RequestParam(value = "pageNum") Integer pageNum,
                                  @RequestParam(value = "pageSize") Integer pageSize,
                                  @RequestParam(value = "ruleID",required=false) String ruleID,
                                  @RequestParam(value = "ruleName",required=false) String ruleName){
        try{
            PageInfo pager =  ruleService.getRuleList(pageNum,pageSize,ruleID,ruleName);
            return new Result(pager);
        }catch (Exception e){
            log.error("分页获取规则列表失败", e);
            return new Result(false, "分页获取规则列表失败");
        }
    }

}
